package nexus.com.button
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import nexus.base.NXSprite;
	
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class Button extends NXSprite
	{
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		private var _label : String = "";
		private var _textField : TextField;
		private var _icon : MovieClip;
		private var _defaultGap : Number = 5;
		private var _btnClip : MovieClip;
		private var _dropShadowFilter : DropShadowFilter;
		private var _isAutoSize : Boolean = true;
		
		
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function Button()
		{
			super();
			
			viewWidth = 40;
			viewHeight = 20;
			
			addChild( btnClip );
		}
			
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------
		
		
		public function get isAutoSize():Boolean
		{
			return _isAutoSize;
		}

		public function set isAutoSize(value:Boolean):void
		{
			_isAutoSize = value;
		}

		public function get textField():TextField
		{
			if( _textField == null )
			{
				var tf : TextFormat = new TextFormat( "돋움", 12, 0x666666 );
				
				_textField = new TextField();
				_textField.autoSize = TextFieldAutoSize.LEFT;
				_textField.setTextFormat( tf );
				_textField.defaultTextFormat = tf;
				
				_textField.mouseEnabled = false;
				_textField.mouseWheelEnabled = false;
				
				addChild( _textField );
			}
			
			return _textField;
		}

		public function set textField(value:TextField):void
		{
			if( _textField && this.contains( _textField ) )
			{
				removeChild( _textField );
				_textField = null;
			}

			_textField = value;
			_textField.mouseEnabled = false;
			_textField.mouseWheelEnabled = false;
			addChild( _textField );
		}

		public function get defaultGap():Number
		{
			return _defaultGap;
		}

		public function set defaultGap(value:Number):void
		{
			_defaultGap = value;
		}

		public function get dropShadowFilter():DropShadowFilter
		{
			if( _dropShadowFilter == null )
			{
				_dropShadowFilter = new DropShadowFilter( 4,45,0xcccccc,0.4 );
			}
			
			return _dropShadowFilter;
		}

		public function set dropShadowFilter(value:DropShadowFilter):void
		{
			_dropShadowFilter = value;
		}

		public function get btnClip():MovieClip
		{
			if( _btnClip == null )
			{
				_btnClip = new MovieClip();
				var g : Graphics = _btnClip.graphics;
				g.beginFill( 0xffffff, 1 );
				g.lineStyle( 1, 0xcccccc, 1 );
				g.drawRect(0,0,viewWidth,viewHeight );
				g.endFill();
				
				_btnClip.filters = [dropShadowFilter];
				
				if( contains( _btnClip ) == false ) addChildAt( _btnClip, 0 );
				
				_btnClip.buttonMode = true;
				_btnClip.addEventListener( MouseEvent.ROLL_OVER, onBtnOver );
				_btnClip.addEventListener( MouseEvent.ROLL_OUT, onBtnOut );
				_btnClip.addEventListener( MouseEvent.MOUSE_DOWN, onBtnDown );
				_btnClip.addEventListener( MouseEvent.MOUSE_UP, onBtnUp );
				_btnClip.addEventListener( MouseEvent.CLICK, onBtnClick );
			}
			
			return _btnClip;
		}

		public function set btnClip(value:MovieClip):void
		{
			if( _btnClip != null && contains( _btnClip ) )
			{
				_btnClip.removeEventListener( MouseEvent.ROLL_OVER, onBtnOver );
				_btnClip.removeEventListener( MouseEvent.ROLL_OUT, onBtnOut );
				_btnClip.removeEventListener( MouseEvent.MOUSE_DOWN, onBtnDown );
				_btnClip.removeEventListener( MouseEvent.MOUSE_UP, onBtnUp );
				_btnClip.removeEventListener( MouseEvent.CLICK, onBtnClick );
				removeChild( _btnClip );
				_btnClip = null;
			}
			
			_btnClip = value;
			
			_btnClip.filters = [dropShadowFilter];
			addChildAt( _btnClip , 0 );
			_btnClip.buttonMode = true;
			_btnClip.addEventListener( MouseEvent.ROLL_OVER, onBtnOver );
			_btnClip.addEventListener( MouseEvent.ROLL_OUT, onBtnOut );
			_btnClip.addEventListener( MouseEvent.MOUSE_DOWN, onBtnDown );
			_btnClip.addEventListener( MouseEvent.MOUSE_UP, onBtnUp );
			_btnClip.addEventListener( MouseEvent.CLICK, onBtnClick );
		}

		public function get icon():MovieClip
		{
			return _icon;
		}
		
		public function set icon(value:MovieClip):void
		{
			if( _icon != null && contains( _icon ) )
			{
				removeChild( _icon );
				_icon = null;
			}
			
			_icon = value;
			addChild( _icon );
		}
		
		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			_label = value;
			textField.text = _label;
			textField.height = textField.textHeight + 4;
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		
		

		override public function updateDisplayList():void
		{
			super.updateDisplayList();
			
			if( _icon ) textField.x = icon.x + icon.width + defaultGap;
			else textField.x = defaultGap;
			
			if( _isAutoSize )
			{
				var currentWidth : Number = textField.x + textField.width + defaultGap;
				var currentHeight : Number = textField.textHeight + defaultGap*2;
				
				viewWidth = currentWidth;
				viewHeight = currentHeight;
			}
			
			btnClip.width = viewWidth;
			btnClip.height = viewHeight;
			textField.y = ( btnClip.height - textField.height ) / 2;
		}
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		protected function onBtnClick(e:MouseEvent):void
		{
			
		}
		
		protected function onBtnOut(e:MouseEvent):void
		{
			
		}
		
		protected function onBtnOver(e:MouseEvent):void
		{
			
		}		
		
		protected function onBtnUp(e:MouseEvent):void
		{
			btnClip.filters = [dropShadowFilter];
		}
		
		protected function onBtnDown(e:MouseEvent):void
		{
			btnClip.filters = [];
		}	
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		
		override public function destory():void
		{
			super.destory();
		}
	}
}